<?php
class SecQuestion extends Controller 
{
	var $uid;
	var $tpl;

	function setVars()
	{
		$this->uid = $this->session->userdata('uid');
		$this->tpl['title'] = "Security Question";
		$this->tpl['head'] = '';
		$this->count = 0;
	}
	
	function SecQuestion()
	{
		parent::Controller();
		$this->setVars();
		log_message('debug', "SecQuestion: Controler Class Initialized");
	}


/*****************************************************************\
|               Sequrity Question
\*****************************************************************/	

	function index() 
	{
		$this->load->model('pdb');

		if(isset($_POST['sq']))
		{
			$this->postCheck();
		}
		$this->QAquery();
		$sq = $this->security_options();
		$this->tpl['securityQuestions'] = $sq['Questions'];
		$this->tpl['securityAnswers'] = $sq['Answers'];
		$this->tpl['myQA'] = $this->get_security_answers();
		$this->load->view('account/secquestion', $this->tpl);
	}

	function postCheck()
	{
		$this->load->library('validation');
		$rules['Question']		= "trim|required|xss_clean";
		$rules['Dance']			= "trim|xss_clean";
		$rules['FacialFuture']	= "trim|xss_clean";
		$rules['Books']			= "trim|xss_clean";
		$rules['MickeyMouse']	= "trim|xss_clean";
		$rules['Dwarfs']			= "trim|xss_clean";
		$rules['Piano']			= "trim|xss_clean";
		$this->validation->set_rules($rules);
		//$this->validation->set_fields($fields);
		log_message('debug', "Register: Validation Rules set");
		if ($this->validation->run() !== FALSE)
		{
			$q = $this->validation->Question;
			$a = $this->validation->$q;
			$this->pdb->dbPut('user',$this->uid,'Security Question',$q,$a);
/*			$data = array(
				'cat' 	=> 'user',
				'name' 	=> $this->uid,
				'sub' 	=> 'Security Question',
				'value' 	=> $this->validation->Question,
				'ext' 	=> $this->validation->$answer
            );
			$this->db->insert('data', $data);
*/
		}
	}


	function QAquery()
	{
		$sql = "SELECT `name` , `value` , `ext`
		FROM `data`
		WHERE `cat` = 'Select'
		AND `sub` = 'Security Question';";
		$this->QAquery = $this->db->query($sql);
	}

	function get_security_answers()
	{
		$QA = $this->pdb->dbGet('user',$this->uid,'Security Question');
		if($QA){
			$myQA = '<dl>';
			foreach($QA as $QArow) {
				foreach($this->QAquery->result() as $row) {
					$this->count++;
					if($row->name == $QArow->value){
						$myQA .= "<dt for=\"$QArow->value\">$row->value</dt>";
						$a = $row->ext[$QArow->ext-1];
						$myQA .= "<dd>$a</dd>";
					}
				}
			}
			$myQA .= '</dl>';
			return $myQA;
		}
	}
	
   	    	 
   function security_options()
	{
		$Question = $Answer = '';
		$Question = '<select name="Question"'; 
		$Question .= "onchange=\"secShowHide(this.options[this.selectedIndex].value)\"
		onmouseover=\"secHide(this.options[this.selectedIndex].value)\">";
		$Question .= "\t<option value=\"\">--Select--</option>\n";
		foreach($this->QAquery->result() as $row) {
			$row->ext = explode(', ',$row->ext); 
			$Question .= "\t<option value=\"$row->name\">$row->value</option>\n";
			$Answer .= "<select id=\"$row->name\" style=\"display: none;\" name=\"$row->name\">\n";
			$Answer .= "\t<option value=\"\">--Select--</option>\n";
			foreach($row->ext as $k => $v) {
				$k++;
				$Answer .= "\t<option value=\"$k\">$v</option>\n";
			}
			$Answer .= "</select>\n";
		}
		$Question .= "</select>\n";

		$return['Questions'] = $Question;
		$return['Answers'] = $Answer;
		return $return;
	}			
}
?>
